package com.iwhalecloud.bss.kite.cucc.web.config;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.cucc.web.interceptor.LocalSessionInterceptor;
import com.iwhalecloud.bss.kite.cucc.web.interceptor.UpdateAttrInterceptor;
import com.iwhalecloud.bss.kite.manager.transaction.TransactionWebFilter;
import com.iwhalecloud.bss.kite.web.interceptor.KiteContextInterceptor;
import com.ztesoft.bss.base.interceptor.LocalInterceptor;
import com.ztesoft.zsmart.core.web.log.page.TrackingLogConfig;
import com.ztesoft.zsmart.core.web.log.page.WebPageTrackingInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.Iterator;

@Configuration
public class LocalInterceptorConfig extends WebMvcConfigurerAdapter {

	@Autowired
    private WebPageTrackingInterceptor interceptor;

    @Autowired
    private TrackingLogConfig properties;

	@Override
    public void addInterceptors(InterceptorRegistry registry) {
		//registry.addInterceptor(new LocalSessionInterceptor());
		//registry.addInterceptor(new SessionInterceptor());

		// kite的上下文
        registry.addInterceptor(new KiteContextInterceptor());
        // 国际化
        registry.addInterceptor(new LocalInterceptor());
        // 缓存事务
        registry.addInterceptor(new TransactionWebFilter());

        // 框架埋点日志拦截器
        InterceptorRegistration registration = registry.addInterceptor(this.interceptor);
        if (KiteListUtils.isNotEmpty(this.properties.getExcludeUrls())) {
            Iterator<String> itr = this.properties.getExcludeUrls().iterator();
            while (itr.hasNext()) {
                String url = itr.next();
                registration.excludePathPatterns(new String[] {url});
            }
        }

        // session拦截器
        registry.addInterceptor(new LocalSessionInterceptor()).excludePathPatterns("/swagger-ui.html")
            .excludePathPatterns("/configuration/ui")
            .excludePathPatterns("/swagger-resources/**")
            .excludePathPatterns("/configuration/security")
            .excludePathPatterns("/v2/api-docs")
            .excludePathPatterns("/error")
            .excludePathPatterns("/exception")
            .excludePathPatterns("/exception/**")
            .excludePathPatterns("/webjars/**")
            .excludePathPatterns("/**/favicon.ico")
            .excludePathPatterns("/swagger/submitLogin");

        // 属性拦截器
        registry.addInterceptor(new UpdateAttrInterceptor()).excludePathPatterns("/swagger-ui.html")
            .excludePathPatterns("/configuration/ui")
            .excludePathPatterns("/swagger-resources/**")
            .excludePathPatterns("/configuration/security")
            .excludePathPatterns("/v2/api-docs")
            .excludePathPatterns("/error")
            .excludePathPatterns("/exception")
            .excludePathPatterns("/exception/**")
            .excludePathPatterns("/webjars/**")
            .excludePathPatterns("/**/favicon.ico")
            .excludePathPatterns("/swagger/submitLogin");
	}
}
